package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.ManagementAndRegistrationForm;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;
/**
 * The ManagementAndReistrationControll conatins methods to appoint new management members and adding new user to system
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */

public class ManagementAndReistrationControll implements FormListener{
	/**
	 * the form for appoint new management members
	 */
	private ManagementAndRegistrationForm managementForm;
	/**
	 * the form for adding new user to system
	 */
	private ManagementAndRegistrationForm newUserForm;
	/**
	 * contains all current management members
	 */
	private HashMap<String,String> apooitered;
	/**
	 * contains all workers in system
	 */
	private ArrayList<String> workerlist;
	/**
	 * contains all workers in system and thier uids
	 */
	private  HashMap<String,String> workers  ;

	@Override
	public void onFormCanceled(FormFrame formFrame) {
		if (formFrame==managementForm) {
			managementForm.dispose();
			managementForm=null;
		}
		if (formFrame==newUserForm) {
			newUserForm.dispose();
			newUserForm=null;
		}		
	}
	
	@Override
	public void onFormSubmited(final FormFrame formFrame, ArrayList<Object> values) {
		if (managementForm==formFrame){	
				if (values.get(0).equals(values.get(1))
						||values.get(1).equals(values.get(2))
						||values.get(1).equals(values.get(3))
						  ||values.get(2).equals(values.get(3))
							|| values.get(0).equals(values.get(2))
							||values.get(0).equals(values.get(3))){
					managementForm.showMessege("Appoint different workers please!");
					values.clear();
				}
			else{
				try {
					ArrayList<Object> uids=new ArrayList<Object>();
					java.util.Iterator<Object> itr = values.iterator();
					while(itr.hasNext()) 
						uids.add(workers.get(itr.next()));
					values=uids;
					Client c = Client.getInstance();
					Mssg mssg = new Mssg(Resources.Strings.AppointManagement,values);
					c.sendToServer(new ServerResponseListener() {
						@Override
						public void onServerResponse(Mssg mssg) {
							if (mssg.is(P.error)) {
								managementForm.showMessege((String) mssg.getMessege());
								return;
							}
							JOptionPane.showMessageDialog(managementForm,Resources.Strings.AppointedSuccesfully);
							formFrame.dispose();
							formFrame.dispose();
						}
					},mssg);
				 
				} catch (Exception e) {
					managementForm.showMessege(Resources.Strings.ConnectFailed);
				}
				
			}
		}
		
		else if(formFrame==newUserForm){
			if (values.contains("")){
				newUserForm.showMessege("ALL FIELDS MUST BE FILLED!");
				values.clear();
			}
			else {
			try {
				Client c = Client.getInstance();
				Mssg mssg = new Mssg(Resources.Strings.UserName,values);
				c.sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							newUserForm.showMessege((String) mssg.getMessege());
							return;
						}
						JOptionPane.showMessageDialog(newUserForm,"New user added successfully");
						formFrame.dispose();
						formFrame.dispose();
						managementForm.pack();
					}
				},mssg);
			 
			} catch (Exception e) {
				newUserForm.showMessege(Resources.Strings.ConnectFailed);
			}
		}
		}

	}
/**
 * Gets the worker list from DB and initiate the managementForm
 * if add new user button pressed, initiate the newUserForm
 */
	public void execManagementRegistration() {
		try {
			Mssg msg = new Mssg(P.usersNameByTid,"'worker'");
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@SuppressWarnings("unchecked")
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null, mssg.getMessege());
						return;
						}
					if ((workers = new  HashMap<String,String>((HashMap<String,String>)mssg.getMessege())) != null) {						
						try {
							Mssg msg = new Mssg(ButtonPermission.ManagementRegistration);
							Client.getInstance().sendToServer(new ServerResponseListener() {
								@Override
								public void onServerResponse(Mssg mssg) {
									if (mssg.is(P.error)) {
										JOptionPane.showMessageDialog(null, mssg.getMessege());
										return;
										}
									if ((apooitered =new  HashMap<String,String>((HashMap<String,String>)mssg.getMessege())) != null) {	
										workers.putAll(apooitered);
										workerlist=new ArrayList<String>(workers.keySet());
										managementForm=new ManagementAndRegistrationForm(workerlist,new ActionListener() {
											@Override
											public void actionPerformed(ActionEvent e) {
												managementForm.dispose();
												newUserForm=new ManagementAndRegistrationForm();
												newUserForm.addFormListener(ManagementAndReistrationControll.this);
												newUserForm.showFrame();	
											 }
										});
										managementForm.addFormListener(ManagementAndReistrationControll.this);
										managementForm.showFrame();	
									} else
										JOptionPane.showMessageDialog(null,String.format(Resources.Strings.NoUsersFound));
								}
							}, msg);
							} 
						catch (Exception e) {
							e.printStackTrace();
							}
					}
				}
			}, msg);
			} 
		catch (Exception e) {
			e.printStackTrace();
			}
	}
	
	



}
